package com.liang.oss.utils;

import com.liang.oss.entity.FileInfo;
import io.minio.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
@RequiredArgsConstructor
public class MinioUtil {

    private final MinioClient minioClient;

    /**
     * 创建存储桶
     */
    public void createBucket(String bucketName) throws Exception {
        if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
        log.info("创建存储桶{}成功", bucketName);
    }

    /**
     * 上传文件
     */
    public void upload(InputStream inputStream, String bucketName, String objectName) throws Exception {
        minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .stream(inputStream, -1, Integer.MAX_VALUE)
                        .build());
        log.info("上传文件{}到存储桶{}", objectName, bucketName);
    }

    /**
     * 下载文件
     */
    public InputStream download(String bucketName, String objectName) throws Exception {
        log.info("开始下载文件{}", objectName);
        return minioClient.getObject(
                GetObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .build());
    }

    /**
     * 列出所有存储桶
     */
    public List<String> listBuckets() throws Exception {
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
    }

    /**
     * 列出存储桶中的所有文件
     */
    public List<FileInfo> listObjects(String bucketName) throws Exception {
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
        List<FileInfo> fileInfoList = new ArrayList<>();
        for (Result<Item> result : results) {
            Item item = result.get();
            FileInfo fileInfo = new FileInfo();
            fileInfo.setFileName(item.objectName());
            fileInfo.setObjectName(item.objectName());
            fileInfo.setIsDir(item.isDir());
            fileInfoList.add(fileInfo);
        }
        return fileInfoList;
    }

    /**
     * 删除存储桶
     */
    public void removeBucket(String bucketName) throws Exception {
        minioClient.removeBucket(
                RemoveBucketArgs
                        .builder()
                        .bucket(bucketName)
                        .build());
        log.info("删除存储桶{}成功", bucketName);
    }

    /**
     * 删除文件
     */
    public void removeObject(String bucketName, String objectName) throws Exception {
        minioClient.removeObject(
                RemoveObjectArgs
                        .builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .build());
        log.info("删除文件{}成功", objectName);
    }
}
